package com.gzcc.controller;

import com.gzcc.model.Cart;
import com.gzcc.model.Product;
import com.gzcc.repository.CartRepository;
import com.gzcc.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 描述: 购物车
 *
 * @Author: Mr.Zhong
 * @Date: 2018/6/14
 */
@Controller
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartRepository cartRepository;
    @Autowired
    private ProductRepository productRepository;

    /**
     * 描述: Create by Sidake
     * CartController
     * 函数cartInput
     * 作用：将商品号、用户名及存入时间存入数据库
     * 将前端传入的商品号(itemId)以及用户id(userID)以及创建时间(currentTime)存入数据库
     * 判定购物车表(Cart)是否已存在该商品，如已存在则不做任何操作
     * 返回前台主界面index.btl
     *
     * @param itemId 商品号
     * @return
     * @Author: Mr.Zhong
     * @Date: 2018/6/14
     */
    @RequestMapping(value = "/cartInput", method = RequestMethod.GET)
    public String cartInput(@RequestParam("itemId") String itemId) {
        // Cart isInCart = cartRepository.findByItemIdAndUser(itemId,userId);
        int productNum = 1;
        Cart isInCart = cartRepository.findByItemId(itemId);
        if (isInCart == null) {
            Cart cart = new Cart();
            Date dateAndTime = new Date(System.currentTimeMillis());
            SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String currentTime = date.format(dateAndTime);
            cart.setCreateTime(currentTime);
            cart.setItemId(itemId);
            cart.setProductNum(productNum);
            //cart.setUser(userId);
            cartRepository.save(cart);
        }
        return "redirect:/cart";
    }

    /**
     * 描述: Create by Sidake
     * CartController
     * 函数cart
     * 作用：将放入购物车的商品传到前端
     * 将购物车表中的所有数据读出并传到前端
     * 返回购物车界面shopcart.btl
     *
     * @Author: Mr.Zhong
     * @Date: 2018/6/14
     */
    @RequestMapping()
    public ModelAndView cart() {
        ModelAndView mv = new ModelAndView();
        List<Cart> carts = cartRepository.findAll();
        List<Product> products = new ArrayList<>();
        for (int i = 0; i < carts.size(); i++) {
            Product product = productRepository.findByItemid(carts.get(i).getItemId());
            products.add(product);
        }
        mv.addObject("carts", carts);
        mv.addObject("products", products);
        mv.setViewName("front-end/shopcart.btl");
        return mv;
    }

    /**
     * 描述: Create by Sidake
     * CartController
     * 函数itemDelete
     * 作用：将数据库中的某条数据删除
     * 通过前端传入购物车中的商品号(cartId)在购物车表中找到该条数据并执行输出操作
     * 返回购物车界面shopcart.btl
     *
     * @Author: Mr.Zhong
     * @Date: 2018/6/14
     */
    @RequestMapping(value = "/itemDelete", method = RequestMethod.GET)
    public String itemDelete(@RequestParam("itemId") String productId) {
        Cart cart = cartRepository.findByItemId(productId);
        cartRepository.deleteById(cart.getCartId());
        return "redirect:/cart";
    }

    /**
     * 描述: Create by Sidake
     * BuyController
     * 函数buyNow
     * 作用：点击立即购买时将所选商品存入数据库并返回购买界面
     * 通过前端传入商品号(itemId)在购物车表查询是否有该数据，如没有则在商品表中找到该条数据并存入购物车表
     * 返回购买界面buynow.btl
     *
     * @Author: Mr.Zhong
     * @Date: 2018/6/14
     */
    @RequestMapping("/buyNow")
    public ModelAndView buyNow(@RequestParam("itemId") String itemId) {
        List<Product> products = new ArrayList<>();
        Product product = productRepository.findByItemid(itemId);
        products.add(product);
        Cart IsInCart = cartRepository.findByItemId(itemId);
        if (IsInCart == null) {
            Cart cart = new Cart();
            Date dateAndTime = new Date(System.currentTimeMillis());
            SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String currentTime = date.format(dateAndTime);
            cart.setCreateTime(currentTime);
            cart.setProductNum(1);
            cart.setItemId(itemId);
            cartRepository.save(cart);
            ModelAndView mv = new ModelAndView();
            mv.addObject("products", products);
            mv.addObject("cart", cart);
            mv.setViewName("/front-end/buynow.btl");
            return mv;
        }
        ModelAndView mv = new ModelAndView();
        mv.addObject("products", products);
        mv.addObject("cart", IsInCart);
        mv.setViewName("/front-end/buynow.btl");
        return mv;
    }

    /**
     * 描述: Create by Sidake
     * CartController
     * 函数buyByCart
     * 作用：将购物车表中的数据传入到购买页面
     * 通过前端传入购物车中的商品号(cartId)在购物车表中找到该条数据并执行输出操作
     * 返回购物车界面buynow.btl
     *
     * @Author: Mr.Zhong
     * @Date: 2018/6/14
     */
    @RequestMapping("/buyByCart")
    public ModelAndView buyByCart() {
        List<Cart> cart = cartRepository.findAll();
        List<Product> products = new ArrayList<>();
        for (int i = 1; i < cart.size(); i++) {
            Product product = productRepository.findByItemid(cart.get(i).getItemId());
            products.add(product);
        }

        ModelAndView mv = new ModelAndView();
        mv.addObject("products", products);
        mv.addObject("cart", cart);
        mv.setViewName("/front-end/buynow.btl");
        return mv;
    }

    /**
     * 描述: Create by Sidake
     * CartController
     * 函数buySuccess
     * 作用：将购物车表中的数据删除并修改商品表相对应商品的商品数
     * 通过前端传入商品号(itemId)、购物车号(cartId)和购买数量(buyNumber)，修改商品表中相对应商品数量并在购物车表中找到该条数据并执行删除操作
     * 返回购物车界面buySuccess.btl
     *
     * @Author: Mr.Zhong
     * @Date: 2018/6/14
     */
    @RequestMapping("/buySuccess")
    public String buyOnes() {
        return "front-end/buySuccess.btl";
    }
}
